Skip to content

[Type] Tensor 5: Document and test needs_grad on the factories#525

Closed
hughperkins wants to merge 12 commits intohp/tensor-stork-4from
hp/tensor-stork-5
Closed

[Type] Tensor 5: Document and test needs_grad on the factories#525
hughperkins wants to merge 12 commits intohp/tensor-stork-4from
hp/tensor-stork-5

Conversation

@hughperkins
Copy link
Copy Markdown
Collaborator

needs_grad already passes through the factory **kwargs (PRs 2-3); this PR makes that contract explicit with tests and a user-guide section.

Adds:

  • 6 tests covering grad allocation, primal+grad kernel round-trip on field and ndarray scalar backends, and grad on tensor_vec/tensor_mat field.
  • "Gradients" section in the user guide showing the needs_grad= usage on both backends with a runnable example.

Documented limitation: qd.Vector.ndarray and qd.Matrix.ndarray do not currently accept needs_grad — that's an upstream Quadrants limitation and is noted in a code comment inside the test file.

All tests pass; sphinx build clean.

Issue: #

Brief Summary

copilot:summary

Walkthrough

copilot:walkthrough

needs_grad already passes through the factory **kwargs (PRs 2-3); this PR
makes that contract explicit with tests and a user-guide section.

Adds:
- 6 tests covering grad allocation, primal+grad kernel round-trip on field
  and ndarray scalar backends, and grad on tensor_vec/tensor_mat field.
- "Gradients" section in the user guide showing the needs_grad= usage on
  both backends with a runnable example.

Documented limitation: qd.Vector.ndarray and qd.Matrix.ndarray do not
currently accept needs_grad — that's an upstream Quadrants limitation and
is noted in a code comment inside the test file.

All tests pass; sphinx build clean.
@hughperkins hughperkins force-pushed the hp/tensor-stork-4 branch 2 times, most recently from 2743267 to e1540b7 Compare April 20, 2026 20:59
Drops the 'flexible' prefix from filenames and identifiers introduced
in this branch series so the user-visible names are simply 'tensor'.
Also strips PR-N back-references that will be meaningless once these
PRs land. Touches only files owned by this series (no changes to
external/ or unrelated tests).
- Apply black/ruff auto-fixes to _tensor.py and import ordering
- Add pylint disable=import-outside-toplevel for late imports
- Fix broken anchors #fields/#ndarrays in tensor.md (point to tensor_types.md)
- Add Backend, tensor*, tensor_annotation to test_api.py expected names

Made-with: Cursor
…r-stork-5

Made-with: Cursor

# Conflicts:
#	docs/source/user_guide/tensor.md
#	python/quadrants/_tensor.py
The qd.tensor_vec / qd.tensor_mat factories already forward needs_grad
through **kwargs (PR 3), but qd.Vector.ndarray and qd.Matrix.ndarray
themselves did not accept the keyword. With Backend.NDARRAY in play,
this meant qd.tensor_vec(..., backend=NDARRAY, needs_grad=True) raised
TypeError, breaking the documented per-backend grad contract.

Mirror the scalar qd.ndarray pattern (impl.ndarray:944-951): when
needs_grad=True, allocate a companion grad ndarray of the same dtype
and shape and wire it via _set_grad. Reject non-real element types
with a clear QuadrantsRuntimeError. Silently ignore on the python
backend, matching scalar ndarray.

Tests cover allocation + shape parity, kernel-side primal/grad
roundtrip, and the int-dtype rejection path, on both Vector and
Matrix variants and through the qd.tensor_vec / qd.tensor_mat
dispatchers.
…nsor

Follow-up to the PR-3 privatization of tensor_vec / tensor_mat. The
Vector/Matrix needs_grad tests added in this branch were calling the
now-private qd.tensor_vec / qd.tensor_mat. Update them to the public
qd.Vector.tensor / qd.Matrix.tensor classmethod surface.

No behavioural change — same factories under the hood (Vector.tensor /
Matrix.tensor delegate to _tensor_vec / _tensor_mat).
Replace the sister-test pairs in test_tensor_grad.py with
@pytest.mark.parametrize("backend", [FIELD, NDARRAY]) on a single test
function. Coverage widens for the FIELD side: we now run the
Vector/Matrix grad kernel roundtrips on FIELD as well as NDARRAY (only
NDARRAY had kernel-roundtrip coverage before; FIELD only had grad
allocation asserts).

Tests kept single-backend:
- test_tensor_vec_ndarray_needs_grad_rejects_int_dtype
- test_tensor_mat_ndarray_needs_grad_rejects_int_dtype

  These probe the QuadrantsRuntimeError raised by the new
  Vector.ndarray / Matrix.ndarray needs_grad gate. The FIELD path goes
  through legacy create_field machinery with a different error class
  and message; pinning that here would couple this branch's contract
  to upstream behaviour outside our control.
Matrix.tensor and Vector.tensor classmethods were added in this PR but
not reflected in the expected API snapshot, causing test_api[Matrix] to
fail with an extra 'tensor' attribute.

Made-with: Cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant